package com.liuyi.netty.im.server.handle.cluster.impl.dispose;

import com.liuyi.netty.im.commons.codes.data.DataEvent;
import com.liuyi.netty.im.commons.enums.UserLineStatus;
import com.liuyi.netty.im.server.config.LimManage;
import com.liuyi.netty.im.server.handle.cluster.ClusterMessageDispose;
import com.liuyi.netty.im.server.handle.storage.ChannelStorage;
import com.liuyi.netty.im.server.handle.storage.User;
import io.netty.channel.Channel;

import java.util.ArrayList;
import java.util.List;

/**
 * toId 为userId的时候接收处理
 *
 * @author Mr.Fmy
 * @since 2021/12/25
 */
public class UserClusterMessageDispose implements ClusterMessageDispose {

    @Override
    public List<Channel> dispose(DataEvent data) {
        List<Channel> result = new ArrayList<>();
        String toUserId = data.getToId();
        List<User> users = LimManage.getUserStorage().listUser(toUserId);
        if (users != null) {
            users.forEach(user -> {
                if (user.getUserLineStatus().equals(UserLineStatus.ON)) {
                    String channelId = user.getChannelId();
                    if (ChannelStorage.containsChannelId(channelId)) {
                        result.add(ChannelStorage.get(channelId));
                    }
                }
            });
        }
        return result;
    }
}
